package utils;

import java.sql.*;

public class JDBCUtils {

	// 定义连接数据库的信息
//	public static final String URL = "jdbc:mysql://10.131.8.253:3306/student01";
//	public static final String USERNAME = "student01";
//	public static final String PASSWORD = "123456";

	public static final String URL = "jdbc:mysql://localhost:3306/world";
	public static final String USERNAME = "root";
	public static final String PASSWORD = "123456";

	// 0. 加载驱动
	static {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver"); // 8.x 驱动
//			Class.forName("com.mysql.jdbc.Driver"); // 5.x 驱动
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 1. 连接数据库
	public static Connection getConnection() throws SQLException {
		Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
		return conn;
	}

	// 2. 释放资源
	public static void destory(Connection conn, Statement stmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (stmt != null) {
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}